<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
    <meta name="format-detection" content="telephone=no">
    <meta content="yes" name="apple-touch-fullscreen"/>
    <title>购物车</title>
    <link rel="stylesheet" href="/static/css/base.css?v={$staticVersion}"/>
    <link rel="stylesheet" href="/static/css/groupbuying.css?v={$staticVersion}"/>
    <link rel="stylesheet" href="/static/css/equip.css?v={$staticVersion}"/>
    <style>.dropload-down{text-align: center;padding-top:10px;}</style>
</head>
<body class="bg_gray_light pb50" style="height: auto;">
    {if !empty($cartList)}
    <form>
        <ul class="cart-list">
            {loop $cartList $k $v}
            <li data-aid="{$v['flash_object']['id']}"
                data-stdid="{$v['stand_good_id']}"
                data-ogid="{$v['good_object']['object_goods_id']}"
                data-price="{$v['good_object']['sale_price']}"
            >
                {if $v['flash_object']['status']!=5}
                <span class="lose">失效</span>
                {else}
                <input type="checkbox" name="">
                {/if}
                <img src="{$v['image']}" alt="" onclick="location.href='/show/groupondetail?flashId=$v['flash_object']['id']'" />
                <dl>
                    <dt class="_listnormal" data-stdid="{$v['stand_good_id']}">
                        {$v['flash_object']['title']}
                        {if $v['flash_object']['status']!=5}
                        <a class="_shop_del_event">删除</a>
                        {else}
                        <a class="_shop_edit_event">编辑</a>
                        {/if}
                    </dt>
                    <dt class="_listedit _listeditnum" data-stocknum="{$v['good_object']['num']}" style="display: none">
                        <span>-</span>
                        <input type="text" name="" readonly="readonly"  value="{$v['num']}" placeholder="0">
                        <span>+</span>
                        <p>还剩{$v['good_object']['num']}件</p>
                    </dt>
                    <dd class="color_gray domit" onclick="location.href='/show/groupondetail?flashId=$v['flash_object']['id']'">
                        {loop $v['good_object']['atrr'] $attr}
                            {$attr['value']}
                        {/loop}
                    </dd>
                    <dd class="_listnormal color-red f14">
                        ￥{$v['good_object']['sale_price']}
                        <span class="num">X{$v['num']}</span>
                    </dd>
                    <dd class="_listnormal">
                        运费
                        {if $v['good_object']['express_fee'] >0 }
                            ￥{$v['good_object']['express_fee']}
                        {else}
                            免运费
                        {/if}
                        <span class="repertory color-red" {if $v['num']<=$v['good_object']['num']} style="display:none" {/if}>库存不足</span>
                    </dd>
                </dl>
                <div class="_listedit" style="display: none" data-stdid="{$v['stand_good_id']}">
                    <a class="over">完成</a>
                    <a class="delete _shop_del_event">删除</a>
                </div>
            </li>
            {/loop}
        </ul>
    </form>
    <button class="clear-btn" style="display: none">清空失效商品</button>
    <section class="bottom-box">
        <input class="all-box" type="checkbox" name="">
        <span class="all-choice">全选</span>
        <span class="total-span" style="">合计：￥<em>0</em></span>
        <button class="total-btn">结算（<em>0</em>件商品）</button>
    </section>
    {else}
    <section class="empty-cart">
        <p>你的购物车空空如也，快去装备库逛逛吧！</p>
        <a href="/show/HomeEquipment"><button class="clear-btn">去装备库</button></a>
    </section>
    <section id="commendgear" class="bg_white">
        <h2 class="blue-line f16 line-gray">最新推荐</h2>
        <ul class="youlike pn">
            <div class="content">
                <div class="lists"></div>
            </div>
            <script id="recommendTemplate" type="text/html">
                <% for(var i=0;i<list.length;i++){ %>
                <li>
                    <a href="equipment?gearId=<%=list[i].gearId%>&_track=homerecommend">
                        <img src="<%if(list[i].images){%><%=list[i].images%><%}else{%>/static/images/4.jpg<%}%>">
                        <h6 class="f14 color_dgray omit"><%=list[i].name%></h6>
                        <p class="omit color_gray f12 mt5"><%=list[i].shortDesc%></p>
                        <p class="color_red f12 mt10">
                            <span class="color-red f14">￥<%=list[i].price%></span>
                            <span class="color_gray fr">热度：<%=list[i].viewCounts%></span>
                        </p>
                    </a>
                </li>
                <% } %>
            </script>
        </ul>
    </section>
    {/if}

</body>
<script src="/static/js/jquery.min.js"></script>
<script src="/static/js/show/gear.js?v={$staticVersion}"></script>
<script src="/static/js/show/template-native.js?v={$staticVersion}"></script>
<script src="/static/js/show/dropload.min.js?v={$staticVersion}"></script>

<script>
    if($('.lose').size()>0){
        $('.clear-btn').css('display','');
        $('.clear-btn').on('click',function(){
            var arr = [];
            $('.lose').each(function(){
                var ogid = $(this).parent().attr('data-ogid');
                arr.push(ogid);
                $(this).parent().fadeOut();
            })
            $.ajax({
                url: "/gearWeb/addshopCart?act=del",
                data: { 'ogid': arr.join(',') },
                context: document.body
            }).done(function() {
                toast('清除成功');
                $('.clear-btn').css('display','none');
            }).error(function() {
                toast('error');
            });
        });
    }
    $('._shop_edit_event').on('click',function(){
        var std_id =  $(this).parent().attr('data-stdid');
        var current = $('.cart-list li[data-stdid="'+std_id+'"]');
        $(current).find('._listnormal').css('display','none');
        $(current).find('._listedit').css('display','');
        $(current).addClass('edit');
    });
    $('.over').on('click',function() {
        var aid =  $(this).parent().attr('data-aid');
        var std_id =  $(this).parent().attr('data-stdid');
        var current = $('.cart-list li[data-stdid="'+std_id+'"]');
        $(current).find('._listnormal').css('display','');
        $(current).find('._listedit').css('display','none');
        $(current).removeClass('edit');

        var num = $(current).find('._listeditnum input').val();
        $(current).find('.num').text('X'+num);

        var ogid = $(current).attr('data-ogid');
        $.ajax({
            url: "/gearWeb/addshopCart?act=add",
            data: { 'ogid': ogid, 'num': num,'aid':aid,'inshopcart':true },
            context: document.body
        }).error(function() {
            toast('error');
        });
        dototal();
    });
    $('._listeditnum span').on('click',function () {
        var option = $(this).text();
        var input = $(this).parent().find('input');
        var stocknum = $(this).parent().attr('data-stocknum');

        var num = parseInt(input.val());

        if(option=='+' && num+1<=stocknum){
            input.val(num+1);
        }else if(option=='-'&&num>1){
            input.val(num-1);
        }
        if(parseInt(input.val())>stocknum){
            $(this).parent().parent().find('.repertory').css('display','')
        }else{
            $(this).parent().parent().find('.repertory').css('display','none')
        }
        dototal();
    });
    $('._shop_del_event').on('click',function(){
        if(confirm('确认要删除这个商品吗？')){
            var std_id =  $(this).parent().attr('data-stdid');
            var current = $('.cart-list li[data-stdid="'+std_id+'"]');

            var ogid = $(current).attr('data-ogid');
            $.ajax({
                url: "/gearWeb/addshopCart?act=del",
                data: { 'ogid': ogid },
                context: document.body
            }).done(function(){
                 current.fadeOut();
            }).error(function() {
                toast('error');
            });
        }
    });


    $('.cart-list input[type=checkbox]').on('click',function(){
        dototal();
    });

    $('.all-box').on('click',function(){
        if($(this).attr('checked')){
            $('.cart-list input[type=checkbox]').attr('checked','checked');
        }else{
            $('.cart-list input[type=checkbox]').removeAttr('checked');
        }
        dototal();
    });

    $('.total-btn').on('click',function(){
        var checkobj =  $('.cart-list input:checkbox:checked');
        if(checkobj.size()==0)
            return;

        var gids = [], nums = [] ,aids = [];
        checkobj.each(function(){
            nums.push($(this).parent().find('._listeditnum input').val());
            gids.push($(this).parent().attr('data-ogid'))
            aids.push($(this).parent().attr('data-aid'))
        });

        $.ajax({
            url: "/gearWeb/checkstock?act=add",
            data: { 'ogids': gids.join(','),'nums':nums.join(',')},
            context: document.body,
            dataType:'json'
        }).done(function(data){
            if(data.result<=0){
                toast('商品数量不足或超出购买限制');
            }else {
                window.location = 'joggers://order/goodsDetail?atype=flashSales&goods_id='+gids.join(',')+'&aid=1&num='+nums.join(',');
            }
        }).error(function() {
            toast('error');
        });

    });

    function dototal(){
        var checkobj =  $('.cart-list input:checkbox:checked');
        var price = 0;
        var num = 0;
        checkobj.each(function(){
            var current_price = parseFloat($(this).parent().attr('data-price'));
            var current_num = parseInt($(this).parent().find('._listeditnum input').val());
            price +=current_price*current_num;
            num +=current_num;
        });
        $('.total-span em').text(price.toFixed(2));
        $('.total-btn em').text(num);
        if(checkobj.size()==0){
            $('.total-span em').text("0");
            $('.total-btn em').text("0");
        }
    }


    var page = 1;
    $(function(){
        // 页数
        if($('#commendgear').size()>0) {
            loadrecommend();
            // dropload
            $('.content').dropload({
                scrollArea: window,//默认加载
                loadDownFn: function (me) {
                    if (page++ > 3) {
                        me.lock();
                        me.noData();
                        me.resetload();
                        return;
                    }
                    loadrecommend(me);
                }
            });
        }
    });

    function loadrecommend(me){
        $.ajax({
            type: 'GET',
            url: '/ajax/recommendGear?page='+page,
            dataType: 'json',
            success: function(data){
                var arrLen = data.list.length;
                if(arrLen > 0){
                    $('.lists').append(template('recommendTemplate',{'list':data.list}));
                }else{
                    if(me){
                        me.lock();
                        me.noData();
                    }
                }
                if(me){
                    me.resetload();
                }
            },
            error: function(xhr, type){
                if(me){
                    me.noData();
                    me.resetload();
                }
            }
        });
    }

</script>
{template 'show/web-gearfooter'}
<script type="text/javascript">
    if(typeof jw === "object" ) {
        jw.api('wv_setnav', JSON.stringify({
            "title": '',
            "nav": getNav([1])
        }), '')
    }
</script>